{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://storage.googleapis.com/xlnet/released_models/cased_L-24_H-1024_A-16.zip\n",
    "# !unzip cased_L-24_H-1024_A-16.zip\n",
    "# !wget https://raw.githubusercontent.com/huseinzol05/NLP-Models-Tensorflow/master/text-classification/utils.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlnet\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from tqdm import tqdm\n",
    "import model_utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sentencepiece as spm\n",
    "from prepro_utils import preprocess_text, encode_ids\n",
    "\n",
    "sp_model = spm.SentencePieceProcessor()\n",
    "sp_model.Load('xlnet_cased_L-24_H-1024_A-16/spiece.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/jupyter/.local/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['negative', 'positive']\n",
      "10662\n",
      "10662\n"
     ]
    }
   ],
   "source": [
    "from utils import *\n",
    "from sklearn.cross_validation import train_test_split\n",
    "\n",
    "trainset = sklearn.datasets.load_files(container_path = 'data', encoding = 'UTF-8')\n",
    "trainset.data, trainset.target = separate_dataset(trainset,1.0)\n",
    "print (trainset.target_names)\n",
    "print (len(trainset.data))\n",
    "print (len(trainset.target))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from prepro_utils import preprocess_text, encode_ids\n",
    "\n",
    "def tokenize_fn(text):\n",
    "    text = preprocess_text(text, lower= False)\n",
    "    return encode_ids(sp_model, text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10662/10662 [00:01<00:00, 9333.88it/s]\n"
     ]
    }
   ],
   "source": [
    "MAX_SEQ_LENGTH = 128\n",
    "\n",
    "SEG_ID_A   = 0\n",
    "SEG_ID_B   = 1\n",
    "SEG_ID_CLS = 2\n",
    "SEG_ID_SEP = 3\n",
    "SEG_ID_PAD = 4\n",
    "\n",
    "special_symbols = {\n",
    "    \"<unk>\"  : 0,\n",
    "    \"<s>\"    : 1,\n",
    "    \"</s>\"   : 2,\n",
    "    \"<cls>\"  : 3,\n",
    "    \"<sep>\"  : 4,\n",
    "    \"<pad>\"  : 5,\n",
    "    \"<mask>\" : 6,\n",
    "    \"<eod>\"  : 7,\n",
    "    \"<eop>\"  : 8,\n",
    "}\n",
    "\n",
    "VOCAB_SIZE = 32000\n",
    "UNK_ID = special_symbols[\"<unk>\"]\n",
    "CLS_ID = special_symbols[\"<cls>\"]\n",
    "SEP_ID = special_symbols[\"<sep>\"]\n",
    "MASK_ID = special_symbols[\"<mask>\"]\n",
    "EOD_ID = special_symbols[\"<eod>\"]\n",
    "\n",
    "input_ids, input_masks, segment_ids = [], [], []\n",
    "\n",
    "for text in tqdm(trainset.data):\n",
    "    tokens_a = tokenize_fn(text)\n",
    "    if len(tokens_a) > MAX_SEQ_LENGTH - 2:\n",
    "        tokens_a = tokens_a[:(MAX_SEQ_LENGTH - 2)]\n",
    "        \n",
    "    tokens = []\n",
    "    segment_id = []\n",
    "    for token in tokens_a:\n",
    "        tokens.append(token)\n",
    "        segment_id.append(SEG_ID_A)\n",
    "    tokens.append(SEP_ID)\n",
    "    segment_id.append(SEG_ID_A)\n",
    "    tokens.append(CLS_ID)\n",
    "    segment_id.append(SEG_ID_CLS)\n",
    "    \n",
    "    input_id = tokens\n",
    "    input_mask = [0] * len(input_id)\n",
    "    if len(input_id) < MAX_SEQ_LENGTH:\n",
    "        delta_len = MAX_SEQ_LENGTH - len(input_id)\n",
    "        input_id = [0] * delta_len + input_id\n",
    "        input_mask = [1] * delta_len + input_mask\n",
    "        segment_id = [SEG_ID_PAD] * delta_len + segment_id\n",
    "    \n",
    "    input_ids.append(input_id)\n",
    "    input_masks.append(input_mask)\n",
    "    segment_ids.append(segment_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "kwargs = dict(\n",
    "      is_training=True,\n",
    "      use_tpu=False,\n",
    "      use_bfloat16=False,\n",
    "      dropout=0,\n",
    "      dropatt=0,\n",
    "      init='normal',\n",
    "      init_range=0.1,\n",
    "      init_std=0.02,\n",
    "      clamp_len=-1)\n",
    "\n",
    "xlnet_parameters = xlnet.RunConfig(**kwargs)\n",
    "xlnet_config = xlnet.XLNetConfig(json_path='xlnet_cased_L-24_H-1024_A-16/xlnet_config.json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10662 1066\n"
     ]
    }
   ],
   "source": [
    "epoch = 10\n",
    "batch_size = 10\n",
    "warmup_proportion = 0.1\n",
    "num_train_steps = int(len(input_ids) / batch_size * epoch)\n",
    "num_warmup_steps = int(num_train_steps * warmup_proportion)\n",
    "print(num_train_steps, num_warmup_steps)\n",
    "\n",
    "training_parameters = dict(\n",
    "      decay_method = 'poly',\n",
    "      train_steps = num_train_steps,\n",
    "      learning_rate = 2e-5,\n",
    "      warmup_steps = num_warmup_steps,\n",
    "      min_lr_ratio = 0.0,\n",
    "      weight_decay = 0.00,\n",
    "      adam_epsilon = 1e-8,\n",
    "      num_core_per_host = 1,\n",
    "      lr_layer_decay_rate = 1,\n",
    "      use_tpu=False,\n",
    "      use_bfloat16=False,\n",
    "      dropout=0.0,\n",
    "      dropatt=0.0,\n",
    "      init='normal',\n",
    "      init_range=0.1,\n",
    "      init_std=0.02,\n",
    "      clip = 1.0,\n",
    "      clamp_len=-1,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Parameter:\n",
    "    def __init__(self, decay_method, warmup_steps, weight_decay, adam_epsilon, \n",
    "                num_core_per_host, lr_layer_decay_rate, use_tpu, learning_rate, train_steps,\n",
    "                min_lr_ratio, clip, **kwargs):\n",
    "        self.decay_method = decay_method\n",
    "        self.warmup_steps = warmup_steps\n",
    "        self.weight_decay = weight_decay\n",
    "        self.adam_epsilon = adam_epsilon\n",
    "        self.num_core_per_host = num_core_per_host\n",
    "        self.lr_layer_decay_rate = lr_layer_decay_rate\n",
    "        self.use_tpu = use_tpu\n",
    "        self.learning_rate = learning_rate\n",
    "        self.train_steps = train_steps\n",
    "        self.min_lr_ratio = min_lr_ratio\n",
    "        self.clip = clip\n",
    "        \n",
    "training_parameters = Parameter(**training_parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Model:\n",
    "    def __init__(\n",
    "        self,\n",
    "        dimension_output,\n",
    "        learning_rate = 2e-5,\n",
    "    ):\n",
    "        self.X = tf.placeholder(tf.int32, [None, None])\n",
    "        self.segment_ids = tf.placeholder(tf.int32, [None, None])\n",
    "        self.input_masks = tf.placeholder(tf.float32, [None, None])\n",
    "        self.Y = tf.placeholder(tf.int32, [None])\n",
    "        \n",
    "        xlnet_model = xlnet.XLNetModel(\n",
    "            xlnet_config=xlnet_config,\n",
    "            run_config=xlnet_parameters,\n",
    "            input_ids=tf.transpose(self.X, [1, 0]),\n",
    "            seg_ids=tf.transpose(self.segment_ids, [1, 0]),\n",
    "            input_mask=tf.transpose(self.input_masks, [1, 0]))\n",
    "        \n",
    "        summary = xlnet_model.get_pooled_out(\"last\", True)\n",
    "        print(summary)\n",
    "        \n",
    "        self.logits = tf.layers.dense(summary, dimension_output)\n",
    "        \n",
    "        self.cost = tf.reduce_mean(\n",
    "            tf.nn.sparse_softmax_cross_entropy_with_logits(\n",
    "                logits = self.logits, labels = self.Y\n",
    "            )\n",
    "        )\n",
    "        \n",
    "        self.optimizer, self.learning_rate, _ = model_utils.get_train_op(training_parameters, self.cost)\n",
    "        \n",
    "        correct_pred = tf.equal(\n",
    "            tf.argmax(self.logits, 1, output_type = tf.int32), self.Y\n",
    "        )\n",
    "        self.accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:memory input None\n",
      "INFO:tensorflow:Use float type <dtype: 'float32'>\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "WARNING:tensorflow:From /home/jupyter/xlnet/modeling.py:533: dropout (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use keras.layers.dropout instead.\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:143: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
      "\n",
      "WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
      "For more information, please see:\n",
      "  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
      "  * https://github.com/tensorflow/addons\n",
      "If you depend on functionality not listed there, please file an issue.\n",
      "\n",
      "WARNING:tensorflow:From /home/jupyter/xlnet/modeling.py:67: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use keras.layers.dense instead.\n",
      "Tensor(\"model_1/sequnece_summary/summary/Tanh:0\", shape=(?, 1024), dtype=float32)\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/learning_rate_decay_v2.py:321: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Deprecated in favor of operator or tf.math.divide.\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n"
     ]
    }
   ],
   "source": [
    "dimension_output = 2\n",
    "learning_rate = 2e-5\n",
    "\n",
    "tf.reset_default_graph()\n",
    "sess = tf.InteractiveSession()\n",
    "model = Model(\n",
    "    dimension_output,\n",
    "    learning_rate\n",
    ")\n",
    "\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_assignment_map_from_checkpoint(tvars, init_checkpoint):\n",
    "    \"\"\"Compute the union of the current variables and checkpoint variables.\"\"\"\n",
    "    assignment_map = {}\n",
    "    initialized_variable_names = {}\n",
    "\n",
    "    name_to_variable = collections.OrderedDict()\n",
    "    for var in tvars:\n",
    "        name = var.name\n",
    "        m = re.match('^(.*):\\\\d+$', name)\n",
    "        if m is not None:\n",
    "            name = m.group(1)\n",
    "        name_to_variable[name] = var\n",
    "\n",
    "    init_vars = tf.train.list_variables(init_checkpoint)\n",
    "\n",
    "    assignment_map = collections.OrderedDict()\n",
    "    for x in init_vars:\n",
    "        (name, var) = (x[0], x[1])\n",
    "        if name not in name_to_variable:\n",
    "            continue\n",
    "        assignment_map[name] = name_to_variable[name]\n",
    "        initialized_variable_names[name] = 1\n",
    "        initialized_variable_names[name + ':0'] = 1\n",
    "\n",
    "    return (assignment_map, initialized_variable_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<tf.Variable 'model/transformer/r_w_bias:0' shape=(24, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/r_r_bias:0' shape=(24, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/word_embedding/lookup_table:0' shape=(32000, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/r_s_bias:0' shape=(24, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/seg_embed:0' shape=(24, 2, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_0/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_1/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_2/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_3/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_4/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_5/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_6/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_7/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_8/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_9/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_10/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_11/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_12/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_13/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_14/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_15/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_16/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_17/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_18/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_19/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_20/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_21/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_22/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/rel_attn/q/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/rel_attn/k/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/rel_attn/v/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/rel_attn/r/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/rel_attn/o/kernel:0' shape=(1024, 16, 64) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/rel_attn/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/rel_attn/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/ff/layer_1/kernel:0' shape=(1024, 4096) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/ff/layer_1/bias:0' shape=(4096,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/ff/layer_2/kernel:0' shape=(4096, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/ff/layer_2/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/ff/LayerNorm/beta:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/transformer/layer_23/ff/LayerNorm/gamma:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'model/sequnece_summary/summary/kernel:0' shape=(1024, 1024) dtype=float32_ref>,\n",
       " <tf.Variable 'model/sequnece_summary/summary/bias:0' shape=(1024,) dtype=float32_ref>,\n",
       " <tf.Variable 'dense/kernel:0' shape=(1024, 2) dtype=float32_ref>,\n",
       " <tf.Variable 'dense/bias:0' shape=(2,) dtype=float32_ref>]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tvars = tf.trainable_variables()\n",
    "tvars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "initialized_variable_names = {}\n",
    "checkpoint = 'xlnet_cased_L-24_H-1024_A-16/xlnet_model.ckpt'\n",
    "assignment_map, initialized_variable_names = get_assignment_map_from_checkpoint(tvars, \n",
    "                                                                                checkpoint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/saver.py:1266: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use standard file APIs to check for files with this prefix.\n",
      "INFO:tensorflow:Restoring parameters from xlnet_cased_L-24_H-1024_A-16/xlnet_model.ckpt\n"
     ]
    }
   ],
   "source": [
    "saver = tf.train.Saver(var_list = assignment_map)\n",
    "saver.restore(sess, checkpoint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cross_validation import train_test_split\n",
    "\n",
    "train_input_ids, test_input_ids, train_input_masks, test_input_masks, train_segment_ids, test_segment_ids, train_Y, test_Y = train_test_split(\n",
    "    input_ids, input_masks, segment_ids, trainset.target, test_size = 0.2\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:52<00:00,  2.51it/s, accuracy=0.889, cost=0.22]\n",
      "test minibatch loop: 100%|██████████| 214/214 [00:31<00:00,  6.80it/s, accuracy=0.667, cost=0.857]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 0, pass acc: 0.000000, current acc: 0.806220\n",
      "time taken: 384.4024746417999\n",
      "epoch: 0, training loss: 0.611391, training acc: 0.672399, valid loss: 0.437451, valid acc: 0.806220\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:47<00:00,  2.51it/s, accuracy=1, cost=0.0803]  \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.53it/s, accuracy=0.667, cost=1.33]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 1, pass acc: 0.806220, current acc: 0.823097\n",
      "time taken: 375.50544786453247\n",
      "epoch: 1, training loss: 0.411278, training acc: 0.833744, valid loss: 0.467083, valid acc: 0.823097\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:47<00:00,  2.51it/s, accuracy=1, cost=0.00196] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.53it/s, accuracy=0.667, cost=1.67]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 2, pass acc: 0.823097, current acc: 0.831536\n",
      "time taken: 375.60887002944946\n",
      "epoch: 2, training loss: 0.276815, training acc: 0.928597, valid loss: 0.858144, valid acc: 0.831536\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:47<00:00,  2.51it/s, accuracy=1, cost=0.000285]\n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.53it/s, accuracy=0.667, cost=2.4] \n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time taken: 375.5307664871216\n",
      "epoch: 3, training loss: 0.134745, training acc: 0.971509, valid loss: 1.194831, valid acc: 0.825910\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:47<00:00,  2.51it/s, accuracy=1, cost=2.93e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.54it/s, accuracy=0.667, cost=3.19]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 4, pass acc: 0.831536, current acc: 0.834349\n",
      "time taken: 375.5645327568054\n",
      "epoch: 4, training loss: 0.051069, training acc: 0.990151, valid loss: 1.404300, valid acc: 0.834349\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:47<00:00,  2.51it/s, accuracy=1, cost=6.22e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.53it/s, accuracy=0.667, cost=2.91]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 5, pass acc: 0.834349, current acc: 0.838100\n",
      "time taken: 375.5147693157196\n",
      "epoch: 5, training loss: 0.021199, training acc: 0.997069, valid loss: 1.297369, valid acc: 0.838100\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:47<00:00,  2.51it/s, accuracy=1, cost=6.77e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.53it/s, accuracy=0.667, cost=2.99]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 6, pass acc: 0.838100, current acc: 0.842788\n",
      "time taken: 375.5234203338623\n",
      "epoch: 6, training loss: 0.014410, training acc: 0.997890, valid loss: 1.354377, valid acc: 0.842788\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:47<00:00,  2.51it/s, accuracy=1, cost=6.59e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.55it/s, accuracy=0.667, cost=2.97]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time taken: 375.47503328323364\n",
      "epoch: 7, training loss: 0.012987, training acc: 0.998359, valid loss: 1.340676, valid acc: 0.841850\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:46<00:00,  2.51it/s, accuracy=1, cost=7.19e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.53it/s, accuracy=0.667, cost=2.93]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 8, pass acc: 0.842788, current acc: 0.846070\n",
      "time taken: 375.2023208141327\n",
      "epoch: 8, training loss: 0.007083, training acc: 0.998945, valid loss: 1.413686, valid acc: 0.846070\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:46<00:00,  2.51it/s, accuracy=1, cost=1.29e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.53it/s, accuracy=0.667, cost=3.54]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time taken: 375.2044596672058\n",
      "epoch: 9, training loss: 0.008962, training acc: 0.998710, valid loss: 1.581032, valid acc: 0.831536\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:46<00:00,  2.51it/s, accuracy=1, cost=1.29e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.54it/s, accuracy=0.667, cost=3.59]\n",
      "train minibatch loop:   0%|          | 0/853 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time taken: 375.1303505897522\n",
      "epoch: 10, training loss: 0.004403, training acc: 0.999648, valid loss: 1.591679, valid acc: 0.839975\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "train minibatch loop: 100%|██████████| 853/853 [05:46<00:00,  2.51it/s, accuracy=1, cost=1.09e-5] \n",
      "test minibatch loop: 100%|██████████| 214/214 [00:28<00:00,  7.55it/s, accuracy=0.667, cost=3.7] "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time taken: 375.21440172195435\n",
      "epoch: 11, training loss: 0.001699, training acc: 0.999883, valid loss: 1.586918, valid acc: 0.841381\n",
      "\n",
      "break epoch:12\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "import time\n",
    "\n",
    "EARLY_STOPPING, CURRENT_CHECKPOINT, CURRENT_ACC, EPOCH = 3, 0, 0, 0\n",
    "\n",
    "while True:\n",
    "    lasttime = time.time()\n",
    "    if CURRENT_CHECKPOINT == EARLY_STOPPING:\n",
    "        print('break epoch:%d\\n' % (EPOCH))\n",
    "        break\n",
    "\n",
    "    train_acc, train_loss, test_acc, test_loss = 0, 0, 0, 0\n",
    "    pbar = tqdm(\n",
    "        range(0, len(train_input_ids), batch_size), desc = 'train minibatch loop'\n",
    "    )\n",
    "    for i in pbar:\n",
    "        index = min(i + batch_size, len(train_input_ids))\n",
    "        batch_x = train_input_ids[i: index]\n",
    "        batch_masks = train_input_masks[i: index]\n",
    "        batch_segment = train_segment_ids[i: index]\n",
    "        batch_y = train_Y[i: index]\n",
    "        acc, cost, _ = sess.run(\n",
    "            [model.accuracy, model.cost, model.optimizer],\n",
    "            feed_dict = {\n",
    "                model.Y: batch_y,\n",
    "                model.X: batch_x,\n",
    "                model.segment_ids: batch_segment,\n",
    "                model.input_masks: batch_masks\n",
    "            },\n",
    "        )\n",
    "        assert not np.isnan(cost)\n",
    "        train_loss += cost\n",
    "        train_acc += acc\n",
    "        pbar.set_postfix(cost = cost, accuracy = acc)\n",
    "        \n",
    "    pbar = tqdm(range(0, len(test_input_ids), batch_size), desc = 'test minibatch loop')\n",
    "    for i in pbar:\n",
    "        index = min(i + batch_size, len(test_input_ids))\n",
    "        batch_x = test_input_ids[i: index]\n",
    "        batch_masks = test_input_masks[i: index]\n",
    "        batch_segment = test_segment_ids[i: index]\n",
    "        batch_y = test_Y[i: index]\n",
    "        acc, cost = sess.run(\n",
    "            [model.accuracy, model.cost],\n",
    "            feed_dict = {\n",
    "                model.Y: batch_y,\n",
    "                model.X: batch_x,\n",
    "                model.segment_ids: batch_segment,\n",
    "                model.input_masks: batch_masks\n",
    "            },\n",
    "        )\n",
    "        test_loss += cost\n",
    "        test_acc += acc\n",
    "        pbar.set_postfix(cost = cost, accuracy = acc)\n",
    "\n",
    "    train_loss /= len(train_input_ids) / batch_size\n",
    "    train_acc /= len(train_input_ids) / batch_size\n",
    "    test_loss /= len(test_input_ids) / batch_size\n",
    "    test_acc /= len(test_input_ids) / batch_size\n",
    "\n",
    "    if test_acc > CURRENT_ACC:\n",
    "        print(\n",
    "            'epoch: %d, pass acc: %f, current acc: %f'\n",
    "            % (EPOCH, CURRENT_ACC, test_acc)\n",
    "        )\n",
    "        CURRENT_ACC = test_acc\n",
    "        CURRENT_CHECKPOINT = 0\n",
    "    else:\n",
    "        CURRENT_CHECKPOINT += 1\n",
    "        \n",
    "    print('time taken:', time.time() - lasttime)\n",
    "    print(\n",
    "        'epoch: %d, training loss: %f, training acc: %f, valid loss: %f, valid acc: %f\\n'\n",
    "        % (EPOCH, train_loss, train_acc, test_loss, test_acc)\n",
    "    )\n",
    "    EPOCH += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
